package com.yonyou.crm.sys.modules.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sys.modules.entity.BiztypeVO;
import com.yonyou.crm.sys.modules.rmitf.IBiztypeRmService;
import com.yonyou.crm.sys.role.entity.RoleVO;

@RestController
@RequestMapping(value = "/sys")
public class BiztypeCtrl {
	
	@Autowired
	private IBiztypeRmService service;

	
	/**
	 * 业务类型列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/modules/{objId}/biztypes", method = RequestMethod.GET)
	public Object getBizTypeList(HttpServletResponse response, @PathVariable("objId") Long objId){

		List<BiztypeVO>biztypes = service.getBiztypeList(objId);	

		List<Object> resultList = new ArrayList<Object>();

		for (BiztypeVO biztype: biztypes) {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			Map<String, Object> dataMap = new HashMap<String, Object>();

			dataMap.put("id", biztype.getId());
			dataMap.put("name", biztype.getName());
			dataMap.put("description", biztype.getDescription());
			dataMap.put("isDefault", biztype.getIsDefault());
			dataMap.put("isCustom", biztype.getIsCustom());
			dataMap.put("isEnabled", biztype.getIsEnabled());
			Map<String, Object> oprMap = getOprMap(biztype);
			dataMap.put("roles", biztype.getRoles() );
			resultMap.put("data", dataMap);
			resultMap.put("operations", oprMap);
			resultList.add(resultMap);
		}
		JSONObject result = new JSONObject();
		result.put("data", resultList);
		return ResponseUtil.getObject(result);
	}
	
	/**
	 * 新增业务类型
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/modules/{objId}/biztypes", method = RequestMethod.POST)
	public Object addBiztype(@PathVariable("objId") Long objId,
			HttpServletRequest request, HttpServletResponse response) {
		BiztypeVO biztype = RequestUtil.getVO(request, BiztypeVO.class);
		biztype.setRoleIds(getLongList(biztype.getRoleIds()));
		biztype.setObjId(objId);
		biztype.setIsCustom(1);
		biztype.setIsDefault(0);
		biztype.setIsEnabled(1);
		biztype = service.addBiztype(biztype);
		JSONObject result = new JSONObject();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("id", biztype.getId());
		dataMap.put("name", biztype.getName());
		dataMap.put("description", biztype.getDescription());
		dataMap.put("isDefault", 0);
		dataMap.put("isCustom", 1);
		dataMap.put("isEnabled", 1);
		Map<String, Object> oprMap = getOprMap(biztype);
		dataMap.put("roles", biztype.getRoles());
		resultMap.put("data", dataMap);
		resultMap.put("operations", oprMap);
		result.put("data", resultMap);
		return ResponseUtil.getObject(result);
	}
	
	/**
	 * 修改业务类型
	 * @param bizTypeId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/biztypes/{biztypeId}", method = RequestMethod.PUT)
	public Object updateBiztype(@PathVariable("biztypeId") Long biztypeId,
			HttpServletRequest request, HttpServletResponse response){
		BiztypeVO biztype = RequestUtil.getVO(request, BiztypeVO.class);
		biztype.setId(biztypeId);
		biztype.setRoleIds(getLongList(biztype.getRoleIds()));
		biztype = service.updateBiztype(biztype);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("id", biztype.getId());
		dataMap.put("name", biztype.getName());
		dataMap.put("description", biztype.getDescription());
		dataMap.put("isDefault", biztype.getIsDefault());
		dataMap.put("isCustom", biztype.getIsCustom());
		dataMap.put("isEnabled", 1);
		Map<String, Object> oprMap = getOprMap(biztype);
		dataMap.put("roles", biztype.getRoles() );
		resultMap.put("data", dataMap);
		resultMap.put("operations", oprMap);
		JSONObject result = new JSONObject();
		result.put("data", resultMap);
		return ResponseUtil.getObject(result);
	}
	
	/**
	 * 业务类型详情
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/biztypes/{id}", method = RequestMethod.GET)
	public Object getBiztype(@PathVariable("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		
		BiztypeVO biztype = service.getBiztype(id);	

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("id", biztype.getId());
		resultMap.put("name", biztype.getName());
		resultMap.put("description", biztype.getDescription());
		resultMap.put("isDefault", biztype.getIsDefault());
		resultMap.put("isCustom", biztype.getIsCustom());
		resultMap.put("isEnabled", biztype.getIsEnabled());
		List<Map<String, Object>> rolesList = new ArrayList<Map<String,Object>>();
		if ( (biztype.getRoles() != null)) {
			Map<String, Object> roleMap = new HashMap<String, Object>();
			for (RoleVO role: biztype.getRoles()) {
				roleMap.put("id", role.getId());
				roleMap.put("name", role.getName());
				rolesList.add(roleMap);
			}
		}
		resultMap.put("roles", rolesList);
		JSONObject result = new JSONObject();
		Map<String, Object> oprMap = getOprMap(biztype);
		result.put("data", resultMap);
		result.put("operations", oprMap);
		return ResponseUtil.getObject(result);
	}
	
	/**
	 * 业务类型删除
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/biztypes/{id}", method = RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		service.deleteBiztype(id);
		return ResponseUtil.getObject();
	}
	
	/**
	 * 业务类型停用启用
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/biztypes/{id}/status", method = RequestMethod.PUT)
	public Object enableOrDisable(@PathVariable("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Integer status = paramObject.getInt("status");
		if (status == 0) {
			service.disableBiztype(id);
		} else {
			service.enableBiztype(id);			
		}
		return ResponseUtil.getObject();
	}
	
	private Map<String, Object>   getOprMap(BiztypeVO biztype) {
		Map<String, Object> oprMap = new HashMap<String, Object>();
		Integer deletable = 1;
		Integer canChangeStauts = 1;
		Integer canAssignRoles = 1;
		if (biztype.getIsDefault() == 1) {
			deletable = 0;
			canAssignRoles = 0;
			canChangeStauts = 0;
		} else 	if (biztype.getIsCustom() == 0) {
			deletable = 0;
		}
		oprMap.put("edit", 1);
		oprMap.put("delete", deletable);
		oprMap.put("changeEnableStatus", canChangeStauts);
		oprMap.put("assignRoles", canAssignRoles);
		return oprMap;
	}
	
	private <T> List<Long> getLongList (List<T> paramList) {
		List<Long> longList = new ArrayList<Long>(); 
		if (paramList != null) {
			for (int i =0;i<paramList.size();i++) {
					longList.add(Long.parseLong(paramList.get(i).toString()));
			}
			return longList;
		} else {
			return null;
		}
		
	}
}
